package com.wanyang.service;

import com.wanyang.dao.MemberDao;
import com.wanyang.dos.GoldRecordDO;
import com.wanyang.dos.MemberSignDO;
import com.wanyang.dos.MemberSignDOExample;
import com.wanyang.mapper.GoldRecordDOMapper;
import com.wanyang.mapper.MemberSignDOMapper;
import com.wanyang.utils.DateUtil;
import com.wanyang.vo.SignVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MemberSignServiceImpl implements MemberSignService {

    @Resource
    private MemberSignDOMapper memberSignDOMapper;

    @Resource
    private MemberDao memberDao;

    @Resource
    private GoldRecordDOMapper goldRecordDOMapper;

    @Override
    public List<SignVO> thisWeekSignRecord(Integer memberId) {
        List<String> thisWeekDates = DateUtil.getThisWeekDates(new Date());
        MemberSignDOExample example = new MemberSignDOExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andSignTimeIn(thisWeekDates);
        example.setOrderByClause("sign_time");
        List<MemberSignDO> memberSignDOs = memberSignDOMapper.selectByExample(example);

        Map<String, MemberSignDO> signDOMap = memberSignDOs.stream()
                .collect(Collectors.toMap(MemberSignDO::getSignTime, a -> a, (k1, k2) -> k1));

        List<SignVO> signVOs = new ArrayList<>();

        for (String thisWeekDate : thisWeekDates) {
            MemberSignDO memberSignDO = signDOMap.get(thisWeekDate);
            SignVO signVO = new SignVO();
            signVO.setDate(thisWeekDate);
            signVO.setSign(memberSignDO != null);
            signVOs.add(signVO);
        }

        return signVOs;
    }

    @Override
    @Transactional
    public void signed(Integer memberId, String signedDate) {
        List<String> thisWeekDates = DateUtil.getThisWeekDates(new Date());

        if (thisWeekDates.contains(signedDate)) {
            MemberSignDOExample example = new MemberSignDOExample();
            example.createCriteria()
                    .andMemberIdEqualTo(memberId)
                    .andSignTimeEqualTo(signedDate);

            long count = memberSignDOMapper.countByExample(example);
            if (count != 1) {
                MemberSignDO memberSignDO = new MemberSignDO();
                memberSignDO.setMemberId(memberId);
                memberSignDO.setCreateTime(DateUtil.currentUnixTime());
                memberSignDO.setSignTime(signedDate);
                memberSignDOMapper.insertSelective(memberSignDO);

                // TODO 读取后台规则查询出当前应加的金币， 先写死
                GoldRecordDO goldRecordDO = new GoldRecordDO();
                goldRecordDO.setGold(new BigDecimal(10));
                goldRecordDO.setCreateTime(DateUtil.currentUnixTime());
                goldRecordDO.setRemark(signedDate + " 签到");
                goldRecordDO.setMemberId(memberId);
                goldRecordDOMapper.insertSelective(goldRecordDO);

                memberDao.operateGold(memberId, new BigDecimal(10));

            }

        }

    }
}
